<template>
  <div class="login_container">
    <!-- 一级路由登录 -->
    <el-row>
      <el-col :span="12" :xs="0"></el-col>
      <el-col :span="12" :xs="24">
        <!-- <el-form class="login_form" :module="loginForm" :rules="rules" ref="loginForms">啊啊啊啊啊啊啊啊啊啊啊啊是:model啊啊啊啊啊啊啊啊西八(扇自己一巴掌) -->
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginForms">
          <h1>Hello</h1>
          <h2>欢迎来到硅谷甄选</h2>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" :prefix-icon="Lock" v-model="loginForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button :loading="loading" type="primary" class="login_btn" @click="login">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue';
import { reactive, ref } from 'vue';
import { useUserStore } from '@/stores/user';
import { useRouter } from 'vue-router';
import { useRoute } from 'vue-router';
import { ElNotification } from 'element-plus';
import { getTime } from '@/utils/time';
// 解决ElNotification样式丢失问题 **重要**
import "element-plus/theme-chalk/el-notification.css";

const route = useRoute()
const router = useRouter()
const userStore = useUserStore()
// 定义变量控制按钮加载效果
let loading = ref(false)
// 收集账号与密码的数据
let loginForm = reactive({
  username:'admin',
  password:'111111'
})
// 自定义校验函数
const validatorUserName = (rule: any, value: any, callback: any) => {
  // rule:校验规则对象
  // value:表单元素文本内容
  // callback:放行函数
  if (value.length >= 5 && value.length <= 10) {
    callback()
  } else {
    callback(new Error('账号长度为5~10'))
  }
}
const validatorPassWord = (rule: any, value: any, callback: any) => {
  if (value.length >= 6 && value.length <= 15) {
    callback()
  } else {
    callback(new Error('密码长度为6~10'))
  }
}
// 定义表单校验规则对象
const rules = {
  // username:[
  //   { required: true, message:'账号不能为空',trigger:'change'},
  //   {min:5,max:10,message:'账号长度为5~10位',trigger:'change'}
  //   ],
  // password:[{required:true,min:6,max:15,message:'密码长度为6~15位',trigger:'change'}]

  // 自定义校验规则：
  username:[{trigger:'change',validator:validatorUserName}],
  password:[{trigger:'change',validator:validatorPassWord}]
}

// 获取el-form组件
const loginForms = ref()
// 登录回调
const login =async ()=>{
  // 保证全部表单项校验通过再发请求
  await loginForms.value.validate()
  // 开始加载
  loading.value = true
  // 1. 通知仓库发登录请求
  // 2. 请求成功->跳转首页展示数据
  // 3. 请求失败->弹出登录失败信息
  try {
    await userStore.userLogin(loginForm)
    // 编程式导航跳转到首页
    // 判断登录的时候，路由路径当中是否有query参数，如果有就往query参数跳转，没有则跳转到首页
    let redirect: any = route.query.redirect
    router.push({path:redirect || '/'})
    // 登录成功提示信息
    ElNotification({
      type:'success',
      message:'登录成功',
      title:`Hi,${getTime()}好`
    })
    // 登录成功，加载效果也消失
    loading.value = false
  } catch (error) {
    // 登录失败，加载效果消失
    loading.value = false
    // 登录失败提示信息
    ElNotification({
      type:'error',
      // 类型断言：user as xxx
      // 类型定义：user: xxx 
      // 类型断言是指将一种类型视作另一种类型，而类型声明则是变量刚开始时就定义它应该是什么类型
      message:(error as Error).message
    })
  }
}
</script>

<style scoped lang="scss">
.login_container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/background.jpg') no-repeat;
  background-size: cover;

  .login_form {
    position: relative;
    width: 50%;
    top: 30vh;
    background: url('@/assets/images/login_form.png') no-repeat;
    background-size: cover;
    padding: 40px;

    h1 {
      color: white;
      font-size: 40px;
    }

    h2 {
      font-size: 20px;
      color: white;
      margin: 20px 0;
    }

    .login_btn {
      width: 100%;
    }
  }
}
</style>